﻿hpxAdminApp.controller('checkCustomerController', function (exportService, $interval, $rootScope, $scope,$timeout, $stateParams, $state, $filter,API_URL, FILE_URL, PHOTO_FILE_URL, fileService, NgTableParams, constantsService, addressService, customerService, payService) {
    console.log($scope)
    var emptyEntity = {};
    var newEntity = angular.copy(emptyEntity, newEntity);

    $scope.filter = {
        'checkedType': 0,        //默认未审核
        'setupTime': '',
        'setupTimes': '',
        'isViewDate': false,
        'bill_status':-1
    };
    //获取票据类型信息
    constantsService.queryConstantsType(3).then(function (data) {
        $scope.customerTypeData = data;
    })
    
    //获取未审核或已审核的客户资料信息
    $scope.tableParams = new NgTableParams({'sorting': {'id': 'desc'}}, {
        getData: function (params) {
            //console.log(params)
            return customerService.queryAllCustomersUnverified(params, $scope.filter.checkedType).then(function (data) {
                $scope.first = $scope.getFirst(params);
                //console.log("审核数据")
                //console.log(data)
                return data;
            });
        }
    });
    //跳转到新页面，放大图片
    $scope.showFront = function () {
        window.open('index.html#/img?path=' + $scope.model.credential_front_photo_path);
    }
    //刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }

    var timer = $interval($scope.reflash, 60 * 1000);
    $scope.$on(
        "$destroy",
        function (event) {
            $interval.cancel(timer);
        }
    );

    $scope.check = function (item) {
        //console.log(item)
        $scope.model = item;        //获取某条数据的详细信息
        if (item.founded_date != null) {
            $scope.model.founded_date = $filter("date")(item.founded_date, "yyyy-MM-dd");
        } else {
            $scope.filter.isViewDate = true;
        }
        //$timeout(function () {
        //    $scope.model.founded_date = $filter("date")(item.founded_date, "yyyy-MM-dd");
        //},10)
        //获取所有的省级地址
        addressService.queryAll().then(function (data) {
            $scope.ProvinceData = data;
            $scope.provinceChange();
        });
        //获取对应省的所有市级地址
        $scope.provinceChange = function () {
            if (!$scope.model.enterprise_province_id) {
                return;
            }
            else {
                return addressService.queryCity($scope.model.enterprise_province_id).then(function (data) {
                    $scope.CityData = data;
                    $scope.cityChange();
                });
            }
        }
        //获取对应市的区
        $scope.cityChange = function () {
            if (!$scope.model.enterprise_city_id) {
                return;
            }
            else {
                return addressService.queryDstrict($scope.model.enterprise_city_id).then(function (data) {
                    $scope.AddressData = data;
                });
            }
        }
        $('#modal-edit').modal('show');     //弹出审核窗口
        $('.jqzoom').imagezoom({
            shiftUp:200
        });       //图片放大功能
    };
    $scope.read = function (item) {
        $scope.model = item;        //获取某条数据的详细信息
        $('#modal-read').modal('show');     //弹出审核窗口
        $('.jqzoom').imagezoom();       //图片放大功能
    };

    //审核通过
    $scope.pass = function () {
        if (confirm('确认通过吗？')) {
            if (!$scope.model.verify_description) {
                $scope.model.verify_description = "审核通过";
            }
            //console.log($scope.model.founded_date)
            //console.log($scope.filter.setupTimes)
            customerService.checkCustomerReview($scope.model.id, {
                'review_result_type_id': 1,
                'unified_social_credit_code': $scope.model.unified_social_credit_code,
                'licence_register_no': $scope.model.licence_register_no,
                'organization_code_number': $scope.model.organization_code_number,
                'licence_addr': $scope.model.licence_addr,
                'legal_name': $scope.model.legal_name,
                'enterprise_name': $scope.model.enterprise_name,
                'contact_person': $scope.model.contact_person,
                'licence_name': $scope.model.licence_name,
                'tax_cert_name':$scope.model.tax_cert_name,
                'legal_cert_no': $scope.model.legal_cert_no,
                'description': $scope.model.verify_description,
                'licence_type': $scope.model.licence_type,
                'founded_date': $scope.filter.setupTimes || $scope.model.founded_date,
                'licence_register_type_id': $scope.model.licence_register_type_id,
                'simple_enterprise_name': $scope.model.simple_enterprise_name
            }).then(function (data) {
                $scope.tableParams.reload();
                $scope.editForm.$setPristine();
                $('#modal-edit').modal('hide');

                //payService.econtractRegister($scope.model.id, $scope.model).then(function (result) {
                //    //console.log("倚天剑账户注册成功")
                //    //倚天鉴系统用户注册
                //});
            });
        }
    };
    //审核不通过
    $scope.reject = function () {
        if (!$scope.model.verify_description || $scope.model.verify_description.length == 0) {
            alert('请填写不通过原因！');
            return;
        }
        if (confirm('确认不通过吗？')) {
            customerService.checkCustomerReview($scope.model.id, {
                'review_result_type_id': -1,
                'unified_social_credit_code': $scope.model.unified_social_credit_code,
                'licence_register_no': $scope.model.licence_register_no,
                'organization_code_number': $scope.model.organization_code_number,
                'licence_addr': $scope.model.licence_addr,
                'legal_name': $scope.model.legal_name,
                'enterprise_name': $scope.model.enterprise_name,
                'contact_person': $scope.model.contact_person,
                'licence_name': $scope.model.licence_name,
                'tax_cert_name': $scope.model.tax_cert_name,
                'legal_cert_no': $scope.model.legal_cert_no,
                'description': $scope.model.verify_description,
                'licence_type': $scope.model.licence_type,
                'founded_date': $scope.filter.setupTimes || $scope.model.founded_date
            }).then(function (data) {
                $scope.tableParams.reload();
                $scope.editForm.$setPristine();
                $('#modal-edit').modal('hide');
            });
        }
    };
    // 重新发送电子合同注册
    $scope.againSend = function (item) {
        if (confirm('确定要重新注册兴业电子合同吗？')) {
            payService.econtractRegister(item.id, item).then(function (result) {
                //console.log("倚天剑账户注册成功")
                //倚天鉴系统用户注册
                window.location.reload();
            });
        }
    }
    //客户失效
    $scope.delete = function (data) {
        if (confirm('确定要删除本客户吗？')) {
            customerService.remove(data.id).then(function (data) {
                $scope.tableParams.reload();
            });
        }
    };

    // 修改企业信息
    $scope.edtiEnterprise = function (item) {
        console.log(item)
        $scope.model = item;        //获取某条数据的详细信息
        $scope.model.founded_date = $filter("date")(item.founded_date, "yyyy-MM-dd");
        $('#modal-editEnterprise').modal('show');     //弹出修改企业窗口
        //$('.jqzoom').imagezoom();       //图片放大功能     
    }
    // 时间插件
    $("#setup_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#setup_time").datetimepicker("setEndDate", $("#setup_time").val());
    }).on('changeDate', function (e) {
        $scope.filter.setupTime = $("#setup_time").val();
    });

    $("#setup_times").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN'
    }).on("click", function (ev) {
        $("#setup_times").datetimepicker("setEndDate", $("#setup_times").val());
    }).on('changeDate', function (e) {
        $scope.filter.setupTimes = $("#setup_times").val();
        $scope.model.founded_date = $scope.filter.setupTimes;
    });

    // 上传图片
    $scope.uploadFiles = function (files, errFiles, successFunc, enter_id) {
        //console.log(files)
        //console.log(enter_id)
        $scope.uploading = true;
        if (errFiles.length > 0) {
            confirm('有文件不符合要求，无法上传！');
        }

        angular.forEach(files, function (file) {
            var fd = new FormData();
            fd.append("file", file);
            var xhr;
            if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
                xhr = new XMLHttpRequest();
            } else if (window.ActiveXObject) { // IE 6 and older
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhr.addEventListener('load', function () {
                if (this.status == 200 || this.status == 304) {
                    //console.log(xhr.responseText)
                    var data2 = JSON.parse(xhr.responseText);
                    //console.log(data2);
                    if (enter_id == 1) {
                        $scope.model.credential_photo_address = data2.data.file_path;
                        $scope.model.credential_photo_id = data2.data.id;
                    } else if (enter_id == 2) {
                        $scope.model.artificial_person_front_photo_address = data2.data.file_path;
                        $scope.model.artificial_person_front_photo_id = data2.data.id;
                    } else if (enter_id == 3) {
                        $scope.model.artificial_person_back_photo_address = data2.data.file_path;
                        $scope.model.artificial_person_back_photo_id = data2.data.id;
                    }
                }
            }, false);
            xhr.open('POST', PHOTO_FILE_URL + '/file', true);
            xhr.send(fd);
        });
    };
    //$('.jqzoom').imagezoom();       //图片放大功能
    //$('.jqzoom_card_front').imagezoom();       //图片放大功能
    //$('.jqzoom_card_back').imagezoom();
    //营业执照图片移除功能
    $scope.removeLicense = function () {
        $scope.model.credential_photo_id = null;
        $scope.model.credential_photo_address = '';
        $('.jqzoom').unbind("mouseenter");
        $('.jqzoom').css('cursor', '');
    }
    // 法人身份证正面移除
    $scope.removeCardFront = function () {
        $scope.model.artificial_person_front_photo_id = null;
        $scope.model.artificial_person_front_photo_address = '';
        $('.jqzoom_card_front').unbind("mouseenter");
        $('.jqzoom_card_front').css('cursor', '');
    }
    // 法人身份证反面移除
    $scope.removeCardBack = function () {
        $scope.model.artificial_person_back_photo_id = null;
        $scope.model.artificial_person_back_photo_address = '';
        $('.jqzoom_card_back').unbind("mouseenter");
        $('.jqzoom_card_back').css('cursor', '');
    }
    // 保存修改信息
    $scope.saveEnterprise = function () {
        customerService.customerReview($scope.model.id, {
            'enterprise_name': $scope.model.enterprise_name,
            'credential_photo_id': $scope.model.credential_photo_id,
            'artificial_person_front_photo_id': $scope.model.artificial_person_front_photo_id,
            'artificial_person_back_photo_id': $scope.model.artificial_person_back_photo_id,
            'description': $scope.model.description,
            'review_result_type_id': 1,
            'licence_register_no': $scope.model.licence_register_no,
            'licence_name': $scope.model.licence_name,
            'licence_type': $scope.model.licence_type,
            'licence_addr': $scope.model.licence_addr,
            'founded_date': $scope.filter.setupTime || $scope.model.founded_date,
            'legal_name': $scope.model.legal_name,
            'legal_cert_no': $scope.model.legal_cert_no,
            'tax_cert_name': $scope.model.tax_cert_name,
            'unified_social_credit_code': $scope.model.unified_social_credit_code,
            'organization_code_number': $scope.model.organization_code_number,
            'licence_register_type_id': $scope.model.licence_register_type_id,
            'simple_enterprise_name': $scope.model.simple_enterprise_name
        }).then(function (data) {
            if (confirm("企业信息修改成功。")) {
                window.location.reload();
                //$('#modal-editEnterprise').modal('hide');
            }
            
        })
    }
    //$scope.saveEnterprise = function () {
    //    alert("1")
    //    customerService.updateEnterprise($scope.model).then(function (data) {
    //        customerService.getAllEnterprise().then(function (data) {
    //            swal("保存成功！");
    //            console.log(data)
    //        })
    //    })
    //}


    //导出表格
    $scope.exportExcel = function () {
        var resource_url = API_URL + "/customers/customerReview?n=65535&orderBy=%2Bid&p=1";
        var sheet_name = "企业审核表";
        var label_names, label_types, label_keys;
        if ($scope.filter.checkedType == 1) {
            resource_url += "&isChecked=1";
            sheet_name += "_已审核";
        } else if ($scope.filter.checkedType == 0) {
            resource_url += "&isChecked=0";
            sheet_name += "_未审核";
        }
        label_names=["企业名称","统一社会信用码/营业执照注册号","联系人","联系号码"];
        label_types=["String","String","String","String"];
        label_keys=["enterprise_name","credential_number","contact_person","contact_phone"];

        var excelRequest = {
            "resource_url": resource_url,
            "resource_name": "enterprises",
            "sheet_name": sheet_name,
            "label_names": label_names,
            "label_types": label_types,
            "label_keys": label_keys
        };
        var token = '';
        if ($rootScope.identity != undefined) {
            token = $rootScope.identity.token;
        }
        exportService.exportExcel('export', token, excelRequest).then(function (result) {
            var newWin = window.open('loading page');
            newWin.location.href = result.data.data;
        })
    }

    //导出表格
    $scope.exportNewExcel = function () {
        var resource_url = API_URL + "/forExcelExport/enterprise?n=10000&orderBy=%2Bid&p=1";
        var sheet_name = "注册企业信息登记表";
        var label_names, label_types, label_keys;
        label_names = ["企业名称","企业ID","更新时间","审核","法人","统一社会信用代码证","组织机构代码证","税务登记证","营业执照","交易经办人", "联系方式", "资金经办人", "联系方式","企业授权委托书","单位授权书",
            "基本户开户行	","基本户对公银行账户","一般户开户行","一般户对公银行账户","	通讯地址","营业执照","法人正面","法人反面"];
        label_types = ["String", "Number", "Date", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String","String",
            "String", "String", "String", "String", "String", "String", "String", "String"];
        label_keys = ["enterprise_name", "enterprise_id", "checked_time", "status", "legal_name", "unified_social_credit_code", "organization_code_number", "tax_registration_code", "licence_register_no",
            "customer_name", "phone_number", "agent_treasurer_name", "agent_treasurer_phone", "shujin", "xybank",
            "bank_number_default", "account_number_default", "bank_number", "account_number", "enterprise_location", "file_path", "fr", "back"];

        var excelRequest = {
            "resource_url": resource_url,
            "resource_name": "enterprises",
            "sheet_name": sheet_name,
            "label_names": label_names,
            "label_types": label_types,
            "label_keys": label_keys
        };
        var token = '';
        if ($rootScope.identity != undefined) {
            token = $rootScope.identity.token;
        }
        exportService.exportExcel('export', token, excelRequest).then(function (result) {
            var newWin = window.open('loading page');
            newWin.location.href = result.data.data;
        })
    }


    // 企业详细信息
    //var oLi = $('.hpx_one')
    //var oDitel = $('.h_detail')
    //$(oLi).each(function (i, ele) {
    //    $(ele).click(function () {
    //        $(this).addClass('h_active').siblings().removeClass('h_active')
    //        var ind = $(this).index()
    //        $(oDitel).eq(ind).addClass('h_block').siblings().removeClass('h_block')
    //    })
    //})
    //if ($('.h_five').find('input[type=text]').attr('value') == "" || $('.h_three').find('input[type=text]').attr('value') == "") {
    //    //$('.h_btn').attr('disabled')
    //    $('.hp_btn').css('display', 'none')
    //    console.log('有一个为空')
    //} else if ($('.h_five').find('input[type=text]').attr('value').length > 0 || $('.h_three').find('input[type=text]').attr('value').length > 0) {
    //    console.log('有一个不为空')
    //}
    //$scope.agent = function () {
    //    window.location.href = "index.html#/app/customer/checkAgentTreasurer"
    //}
});
